﻿/*----------------------------------------------------------------
// Copyright (C) 2011 周士祎
// 版权所有。 
//
// 文件名：MaterialInspectionController
// 文件功能描述：原材料验收
//
// 
// 创建标识：2012-02-22 周士祎
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class MaterialInspectionController : Controller
    {
        //
        // GET: /MaterialInspection/

        public ActionResult MaterialInspectionIndex()
        {
            //ViewData["group"] = GetGroup();
            ViewData["Warehouse_ID"] = GetWarehouse_ID();
            return View();
        }

        private ReportsInfo report = new ReportsInfo();
        private YaxNodeInfo yninfo = new YaxNodeInfo(true);
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroup()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            GroupInfo ginfo = new GroupInfo(true);
            IList<GroupInfo> grouplist = ginfo.SelGroupInfo(100, 1, "");
            foreach (GroupInfo gi in grouplist)
            {
                dic.Add(gi.ID, gi.Group_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public IList<SelectListItem> GetWarehouse_ID()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            GroupInfo ginfo = new GroupInfo(true);
            DataSet ds = ginfo.GetWarehouse_ID();
            dic.Add(-1, "请选择...");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dic.Add(Convert.ToInt32(dr["ID"]), dr["Warehouse_Name"].ToString());
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public IList<SelectListItem> GetWarehouse_ID1()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            GroupInfo ginfo = new GroupInfo(true);
            DataSet ds = ginfo.GetWarehouse_ID1();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dic.Add(Convert.ToInt32(dr["ID"]), dr["Warehouse_Name"].ToString());
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取报表数据
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetMaterialInspection(PageVM pvm)
        {
            string condition = pvm.query;
            string dt = condition.Split(',')[0].ToString();
            int gid = Convert.ToInt32(condition.Split(',')[1]);
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = report.GetMaterialInspectionCount(dt, gid);

            DataSet list = report.GetMaterialInspection(dt, gid, pvm.rp, pvm.page);

            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item[0].ToString());
                v.Add(item[1].ToString());
                v.Add(Convert.ToDateTime(item[2]).ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(item[3].ToString());
                v.Add(item[4].ToString());
                v.Add(item[5].ToString());
                v.Add(Con_GoodsInfo.GetGoodsNameByID(Convert.ToInt32(item[5])));
                v.Add(item[6].ToString());
                v.Add(item[7].ToString());
                v.Add(item[8].ToString());
                v.Add(item[9].ToString());
                v.Add(Convert.ToDateTime(item[10]).ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(item[11].ToString());
                v.Add(item[12].ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item[0].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 输出到Excel
        /// </summary>
        /// <param name="yy"></param>
        /// <param name="mm"></param>
        /// <returns></returns>
        public ActionResult ExprotMaterialInspection(string dt, int gid)
        {
            return EContent(dt, gid);
        }
        public ExcelContentResult EContent(string dt, int gid)
        {
            StringBuilder sBuilder = new StringBuilder();
            int totlerows = report.GetMaterialInspectionCount(dt, gid);
            DataSet list = report.GetMaterialInspection(dt, gid, totlerows, 1);

            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='5' style='font-size:25px; text-align:center;'>食堂原材料验收单</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td style='text-align:right;width:50px;'>日期</td>");
            sBuilder.Append("<td style='text-align:left;width:200px;'>" + dt + "</td>");
            sBuilder.Append("<td style='width:100px;'></td>");
            sBuilder.Append("<td style='text-align:right;width:100px;'>班组</td>");
            sBuilder.Append("<td style='text-align:left;width:100px;'>" + gid + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");

            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td style='width:50px;'>序号</td>");
            sBuilder.Append("<td style='width:200px;'>品名</td>");
            sBuilder.Append("<td style='width:100px;'>数量</td>");
            sBuilder.Append("<td style='width:100px;'>单价</td>");
            sBuilder.Append("<td style='width:100px;'>金额</td>");
            sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
            decimal sumamount = 0.00M;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td style='width:50px;'>" + item[0].ToString() + "</td>");
                sBuilder.Append("<td style='width:200px;'>" + Con_GoodsInfo.GetGoodsNameByID(Convert.ToInt32(item[5])) + "</td>");
                sBuilder.Append("<td style='width:100px;'>" + item[6].ToString() + "</td>");
                sBuilder.Append("<td style='width:100px;'>" + Math.Round(Convert.ToDecimal(item[7]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td style='width:100px;'>" + Math.Round(Convert.ToDecimal(item[8]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
                sumamount = sumamount + Convert.ToDecimal(item[8]);
            }
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td>合计</td>");
            sBuilder.Append("<td>" + sumamount.ToString() + "</td>");
            sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
            sBuilder.Append("</table>");

            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td>记账</td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td>验收</td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td>采购</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "MaterialInspection";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }

        //================================================================
        //  食品原料验收记录
        //================================================================
        public ActionResult MaterialInspectionLog()
        {
            ViewData["Warehouse_ID"] = GetWarehouse_ID1();
            return View();
        }
        /// <summary>
        /// 获取报表数据
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetMaterialInspectionLog(PageVM pvm)
        {
            string condition = pvm.query;
            string dt = condition.Split(',')[0].ToString();
            int gid = Convert.ToInt32(condition.Split(',')[1]);
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = report.GetMaterialInspectionCount(dt, gid);

            DataSet list = report.GetMaterialInspection(dt, gid, pvm.rp, pvm.page);

            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item[0].ToString());
                v.Add(item[5].ToString());
                v.Add(Con_GoodsInfo.GetGoodsNameByID(Convert.ToInt32(item[5])));
                v.Add(item[6].ToString());
                v.Add("");
                v.Add("");
                v.Add(item[9].ToString());
                v.Add("√");
                v.Add("√");
                v.Add(Convert.ToDateTime(item[2]).ToString("MM月dd日", DateTimeFormatInfo.InvariantInfo));
                v.Add("");
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item[0].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        public ActionResult ExportMaterialInspectionLog(string dt, int gid)
        {
            return EContentLog(dt, gid);
        }
        public ExcelContentResult EContentLog(string dt, int gid)
        {
            StringBuilder sBuilder = new StringBuilder();
            int totlerows = report.GetMaterialInspectionCount(dt, gid);
            DataSet list = report.GetMaterialInspection(dt, gid, totlerows, 1);

            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='10' style='font-size:25px; text-align:center;'>上海港餐饮单位（食堂）卫生管理手册</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td colspan='10' style='text-align:left;'>表二：</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td colspan='10' style='text-align:center;'>上海港餐饮单位（食堂）食品原料验收记录</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;font-size:17px;'>");
            sBuilder.Append("<td colspan='10' style='text-align:center;'>振东分公司第一员工食堂</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td style='width:50px; '>序号</td>");
            sBuilder.Append("<td style='width:100px; '>原料<br/>名称</td>");
            sBuilder.Append("<td style='width:80px; '>规格/<br/>数量</td>");
            sBuilder.Append("<td style='width:80px; '>生产日<br/>期(批号)</td>");
            sBuilder.Append("<td style='width:100px; '>保质期</td>");
            sBuilder.Append("<td style='width:100px; '>供货单位<br/>名称</td>");
            sBuilder.Append("<td style='width:100px; '>卫生许可证<br/>/营业执照</td>");
            sBuilder.Append("<td style='width:100px; '>索证<br/>索票</td>");
            sBuilder.Append("<td style='width:100px; '>进货<br/>日期</td>");
            sBuilder.Append("<td style='width:100px; '>验收人<br/>签名</td>");
            sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='white-space: nowrap;font-size:14px;'>");
                sBuilder.Append("<td style='width:50px; '>" + item[0].ToString() + "</td>");
                sBuilder.Append("<td style='width:100px; '>" + Con_GoodsInfo.GetGoodsNameByID(Convert.ToInt32(item[5])) + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + item[6].ToString() + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + " " + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + " " + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + item[9].ToString() + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + "√" + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + "√" + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + Convert.ToDateTime(item[2]).ToString("MM月dd日", DateTimeFormatInfo.InvariantInfo) + "</td>");
                sBuilder.Append("<td style='width:80px; '>" + " " + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("<tr style='white-space: nowrap;font-size:17px;'>");
            sBuilder.Append("<td colspan='10' style='text-align:left;'>注：1、供货单位卫生许可证、营业执照、生产许可证等复印留档。</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;font-size:17px;'>");
            sBuilder.Append("<td colspan='10' style='text-align:left;'>2、索证索票需复印留档。</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "MaterialInspectionLog";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }

        //============================================================
        //  二食堂荤菜、蔬菜、鱼
        //============================================================
        public ActionResult CanteenNonstapleFood()
        {
            ViewData["yy"] = GetOutstockYY();
            ViewData["mm"] = GetOutstockMM();
            ViewData["canteen"] = GetYaxNode();
            return View();
        }

        public ActionResult GetCanteenNonstapleFood(PageVM pvm)
        {
            string condition = pvm.query;
            int yy = Convert.ToInt32(condition.Split(',')[0]);
            int mm = Convert.ToInt32(condition.Split(',')[1]);
            int st = Convert.ToInt32(condition.Split(',')[2]);
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = report.GetCanteenNonstapleFoodCount(yy, mm, st);
            DataSet list = report.GetCanteenNonstapleFood(yy, mm, st, pvm.rp, pvm.page);
            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item[0].ToString());
                v.Add(Convert.ToDateTime(item[1]).ToString("MM月dd日", DateTimeFormatInfo.InvariantInfo));
                v.Add(item[2].ToString());
                v.Add(item[3].ToString());
                v.Add(item[4].ToString());
                v.Add(item[5].ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item[0].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 获取年份列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetOutstockYY()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, (DateTime.Now.Date.Year - 1).ToString());
            dic.Add(2, (DateTime.Now.Date.Year).ToString());
            dic.Add(3, (DateTime.Now.Date.Year + 1).ToString());
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString(),
                    Selected = (m.Value == (DateTime.Now.Date.Year).ToString()) ? true : false
                }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取月份列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetOutstockMM()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "1");
            dic.Add(2, "2");
            dic.Add(3, "3");
            dic.Add(4, "4");
            dic.Add(5, "5");
            dic.Add(6, "6");
            dic.Add(7, "7");
            dic.Add(8, "8");
            dic.Add(9, "9");
            dic.Add(10, "10");
            dic.Add(11, "11");
            dic.Add(12, "12");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString(),
                    Selected = (m.Value == DateTime.Now.Month.ToString()) ? true : false
                }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取食堂
        /// </summary>
        /// <returns></returns>
        public SelectList GetYaxNode()
        {
            IList<YaxNodeInfo> ynlist = yninfo.SelYaxNodeInfo("and CONFLAG=1");
            ynlist.ToList();
            return new SelectList(ynlist, "NodeID", "Nodename");
        }

        public ExcelContentResult EContentCanteenNon(int yy, int mm, int st)
        {
            StringBuilder sBuilder = new StringBuilder();
            int totlerows = report.GetCanteenNonstapleFoodCount(yy, mm, st);
            DataSet list = report.GetCanteenNonstapleFood(yy, mm, st, totlerows, 1);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style=' white-space: nowrap;'>");
            sBuilder.Append("<td colspan='5' style='font-size:25px; text-align:center;'>" + st.ToString() + "食堂荤菜、蔬菜、鱼</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td style='width:100px;'>日期</td>");
            sBuilder.Append("<td style='width:80px;'>荤菜</td>");
            sBuilder.Append("<td style='width:80px;'>蔬菜</td>");
            sBuilder.Append("<td style='width:100px;'>鱼</td>");
            sBuilder.Append("<td style='width:100px;'>合计</td>");
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px;white-space: nowrap;'>");
                sBuilder.Append("<td style='width:80px;'>" + Convert.ToDateTime(item[1]).ToString("MM月dd日", DateTimeFormatInfo.InvariantInfo) + "</td>");
                sBuilder.Append("<td style='width:80px;'>" + Math.Round(Convert.ToDecimal(item[2]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td style='width:80px;'>" + Math.Round(Convert.ToDecimal(item[3]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td style='width:80px;'>" + Math.Round(Convert.ToDecimal(item[4]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td style='width:80px;'>" + Math.Round(Convert.ToDecimal(item[5]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "CanteenNonstapleFood";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
